package com.scut.fy.gateway.service.impl;


import com.scut.fy.common.core.util.Assert;
import com.scut.fy.common.core.util.BeanUtils;
import com.scut.fy.gateway.model.dto.SysUserDto;
import com.scut.fy.gateway.model.vo.OauthLoginVo;
import com.scut.fy.gateway.service.LoginService;
import com.scut.fy.gateway.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @author JY
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SysUserService sysUserService;

    @Override
    public OauthLoginVo pwdLogin(OauthLoginVo loginDto) {
        return null;
    }

    @Override
    public Boolean checkUserPwd(String userName, String password) {
        Assert.notNull(userName, "用户名不能为空");
        Assert.notNull(password, "用户密码不能为空");
        // 先查redis里面有没有这个用户的数据
        SysUserDto userDto = BeanUtils.jsonToBean(
                redisTemplate.opsForValue().get("FY_GATEWAY_SYS_USER"+userName).toString(),
                SysUserDto.class);
        if (null == userDto) {
            // 如果是空，从feign接口查询
            userDto = sysUserService.findUserDtoByUserName(userName);
        }

        if (password.equals(userDto.getPassword())) {
            // 如果有值就缓存在redis里面，30分钟过期
            redisTemplate.opsForValue().set("FY_GATEWAY_SYS_USER"+userName, userDto, 60*30);
            return true;
        } else {
            return false;
        }
    }
}
